﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HomeStayWebCP.Models;
using DAL;
using HomeStayWebCP.Models.BusinessObject;
using HomeStayWebCP.Mailers;

namespace HomeStayWebCP.Controllers.Admin
{
    public class ManageUsersController : Controller
    {
        //
        // GET: /ManageUsers/

        public ActionResult Index()
        {
            UserModel usermodel = new UserModel();
            if (HttpContext.Session["roleID"] != null && !HttpContext.Session["roleID"].ToString().Equals(String.Empty))
            {
                return View();
            }
            else
            {
                //redirect to You doesn't have permission
                return RedirectToAction("Login", "Admin");
            } 
        }
        public ActionResult DatatableListUsers(JQueryDataTableParamModel param)
        {
            UserModel usermodel = new UserModel();
            if (HttpContext.Session["roleID"] != null && !HttpContext.Session["roleID"].ToString().Equals(String.Empty))
            {
                try
                {
                    int roleID = Int32.Parse(HttpContext.Session["roleID"].ToString());
                    var allUser = usermodel.GetListOfUser(roleID);
                    IEnumerable<UserInfo> filteredListUsers;
                    if (!string.IsNullOrEmpty(param.sSearch))
                    {
                        var isUsernameSearchable = Convert.ToBoolean("true");
                        var isRoleSearchable = Convert.ToBoolean("true");
                        var isStatusSearchable = Convert.ToBoolean("true");

                        filteredListUsers = usermodel.GetListOfUser(roleID)
                        .Where(c => isUsernameSearchable && c.Username.ToLower().Contains(param.sSearch.ToLower())
                               ||
                               isRoleSearchable && c.Role.ToLower().Contains(param.sSearch.ToLower())
                               ||
                               isStatusSearchable && c.Status.ToLower().Contains(param.sSearch.ToLower()));
                    }
                    else
                    {
                        filteredListUsers = allUser;
                    }

                    var displayedListUser = filteredListUsers.Skip(param.iDisplayStart).Take(param.iDisplayLength);

                    var result = from c in displayedListUser
                                 select new[]
                                    {
                                        c.Username,
                                        c.Fullname,
                                        c.Role.ToString(),
                                        c.Email,
                                        c.Phone,
                                        c.Status
                                    }
                                    ;
                    return Json(new
                    {
                        sEcho = param.sEcho,
                        iTotalRecords = allUser.Count(),
                        iTotalDisplayRecords = filteredListUsers.Count(),
                        aaData = result
                    }, JsonRequestBehavior.AllowGet);

                }
                catch 
                {
                    return null;
                    //redirect to page error
                }

            }
            else
            {
                //redirect to You doesn't have permission
                return RedirectToAction("Login", "Admin");
            }

            

        }
        public ActionResult ViewUserDetail(string uName)
        {
            UserModel userModel = new UserModel();
            UserDetailInfo userdetail = userModel.getUserOfSystemDetail(uName);
            ViewBag.userdetail = userdetail;
            return View();
        }
        public ActionResult BanUser(String BanUserName, String BanEmail)
        {
            ViewBag.BanUserName = BanUserName;
            ViewBag.BanEmail = BanEmail;
            BanUserObject b = new BanUserObject();
            b.Email = BanEmail;
            b.Username = BanUserName;
            b.Reason = "";
            return View(b);
        }
        public ActionResult ConfirmedBan(BanUserObject obj)
        {
			MyMailer mailer = new MyMailer();
            UserModel usermodel = new UserModel();
            if (HttpContext.Session["roleID"] != null && !HttpContext.Session["roleID"].ToString().Equals(String.Empty))
            {
                try
                {
                    UserModel userModel = new UserModel();
                    int result = userModel.BanUser(obj.Username);
                    String email = obj.Email;
                    String reason = obj.Reason;
                    String username = obj.Username;
                    mailer.MailBanUser(username, reason, email).Send();
                    //send mail to email with reason
                    ViewBag.username = username;
                    return View();
                }
                catch
                {
                    //show error page
                    return null;
                }
            }
            else
            {
                //redirect to You doesn't have permission
                return RedirectToAction("Login", "Admin");
            }
            
        }
    }
}
